#!/bin/bash

COMMENT_BIN=`pwd`/coding-format

OPWD=`pwd`

# dnf install -y bison flex lynx
cd /usr/local/src
 rm -rf /usr/aaa
 rm -rf nextsignal
#git clone -b3.4.11 https://gitee.com/opensource-release/opensips.git nextsignal
tar -zxvf nextsignal.tar.gz
cd nextsignal

CPWD=`pwd`

# 删除无需模块信息
cd modules
rm -rf aaa_diameter aaa_radius auth_aaa auth_jwt \
       b2b_logic b2b_logic_xml b2b_sca benchmark \
       cachedb_cassandra cachedb_couchbase cachedb_local cachedb_memcached cachedb_mongodb cachedb_redis cachedb_sql \
       call_center call_control callops carrierroute cfgutils cgrates compression cpl_c \
       db_berkeley db_cachedb db_flatstore \
       db_mysql db_oracle db_sqlite db_text db_perlvdb db_postgres db_unixodbc db_virtual \
       dialplan diversion dns_cache domainpolicy drouting \
       emergency enum event_datagram event_flatstore event_kafka event_rabbitmq \
       event_route event_stream event_virtual event_xmlrpc \
       fraud_detection freeswitch_scripting \
       gflags group h350 identity imc jabber json jsonrpc ldap lua \
       mangler mathops media_exchange mediaproxy mi_datagram mid_registrar mi_script mi_xmlrpc_ng mmgeoip msilo \
       nat_traversal options osp \
       peering perl permissions pi_http pike presence_callinfo presence_dfks \
       presence_dialoginfo presence_mwi presence_xcapdiff presence_xml \
       proto_tls proto_ws proto_wss proto_bin proto_bins proto_hep proto_sctp proto_smpp \
       pua_bla pua_dialoginfo pua_mi pua_usrloc pua_xmpp python \
       qos qrouting ratelimit regex rabbitmq rabbitmq_consumer rate_cacher rest_client \
       rls rtpengine rtpproxy rtp_relay script_helper sipcapture sip_i siprec \
       statistics prometheus stun sngtc snmpstats speeddial sql_cacher sst stir_shaken \
       topology_hiding tracer \
       uuid uac_redirect uac_registrant userblacklist \
       xcap xcap_client xml xmpp 
       # uac_auth event_routing clusterer freeswitch

cd $CPWD

mv etc/opensips.cfg etc/nextsignal.cfg
mv opensips.8 nextsignal.8
mv opensips.cfg.5 nextsignal.cfg.5
mv modules/registrar/test/opensips.cfg modules/registrar/test/nextsignal.cfg
mv lib/json/opensips_json_c_helper.h lib/json/nextsignal_json_c_helper.h
mv modules/freeswitch modules/nextswitch
mv modules/nextswitch/fs_api.c modules/nextswitch/ns_api.c
mv modules/nextswitch/fs_api.h modules/nextswitch/ns_api.h
mv modules/nextswitch/fs_ipc.c modules/nextswitch/ns_ipc.c
mv modules/nextswitch/fs_ipc.h modules/nextswitch/ns_ipc.h
mv modules/nextswitch/fs_mod.c modules/nextswitch/ns_mod.c
mv modules/nextswitch/fs_proc.c modules/nextswitch/ns_proc.c
mv modules/nextswitch/fs_proc.h modules/nextswitch/ns_proc.h

rm -rf utils/db_berkeley
rm -rf utils/db_oracle
rm -rf NEWS README README.md INSTALL ChangeLog CREDITS AUTHORS COPYING
rm -rf doc modules/*/doc modules/*/README db/doc lib/reg/doc net/proto_tcp/doc net/proto_udp/doc db/schema \
       examples utils aaa/doc packaging \
       net/proto_udp/README net/proto_tcp/README \
       db/example db/README cachedb/example \
       etc/tls etc/dictionary.opensips \
       scripts/db* scripts/mysql scripts/oracle scripts/postgres scripts/sqlite scripts/pi_http \
       opensips.8 opensips.cfg.5 opensips-m4cfg.8 osipsconfig.8 
       # scripts test mem/test parser/test cachedb/test modules/registrar/test
rm -rf nextsignal.8 nextsignal.cfg.5 bdb_recover.8


/bin/cp -f "$OPWD"/Makefile*  ./
rm -f Makefile.test

/bin/cp -f "$OPWD"/README.md  ./
# 去除注释信息
$COMMENT_BIN -w $CPWD -e .c,.h -m 1

SOURCE_PATH="*.h *.c */*.h */*.c */*/*.h */*/*.c test/*.sh test/*.cfg \
        cachedb/test/test_cachedb.c etc/nextsignal.cfg \
        modules/acc/etc/radiusclient.conf test/include/require scripts/build/build.conf.sub \
        modules/nextswitch/Makefile modules/nextswitch/esl/src/include/esl.h \
        modules/nextswitch/ms_api.h modules/nextswitch/ms_proc.h
        .gitignore cfg.lex cfg.y"

sed -i 's#opensips#nextsignal#g' $SOURCE_PATH
sed -i 's#OpenSIPS#NextSignal#g' $SOURCE_PATH
sed -i 's#OPENSIPS#NEXTSIGNAL#g' $SOURCE_PATH
sed -i 's#Opensips#NextSignal#g' $SOURCE_PATH
sed -i 's#FREESWITCH#NEXTSWITCH#g' $SOURCE_PATH
sed -i 's#FreeSWITCH#NextSwitch#g' $SOURCE_PATH
sed -i 's#freeswitch#nextswitch#g' $SOURCE_PATH
sed -i 's#fs_api#ns_api#g' $SOURCE_PATH
sed -i 's#fs_esl#ns_esl#g' $SOURCE_PATH
sed -i 's#fs_ipc#ns_ipc#g' $SOURCE_PATH
sed -i 's#fs_proc#ns_proc#g' $SOURCE_PATH

sed -i 's#dfjrewr12386fd6-343#support#g' modules/usrloc/ul_mi.c

tail -n 250 etc/nextsignal.cfg > etc/nextsignal.cfg.backup
/bin/mv -f etc/nextsignal.cfg.backup etc/nextsignal.cfg

cat lib/turbocompare.h | grep -v 'Twiddling' > lib/turbocompare.h.backup
/bin/mv -f lib/turbocompare.h.backup lib/turbocompare.h

tail -n 1006 test/7.cfg > test/7.cfg.backup
/bin/mv -f test/7.cfg.backup test/7.cfg

echo ""
echo "----------checking--------------"
$COMMENT_BIN -w $CPWD -e .c,.h -m 9
echo ""

# 编译
make all -j4 prefix=/usr/local/nextsignal
# 安装
make install prefix=/usr/local/nextsignal
